//
//  ScreenFuncView.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/6.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class ScreenFuncView: BaseView,reportHandle {

    private var hei : CGFloat = 0 //高度
    var numberSeted = 0
    var delegated : Bool = false //如果需要称为子视图的代理，则设置为true
    
    var completionBlock : ()->Void = {
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func addHead(title : String) -> Void {
        configHead(title: title)
        hei = hei + 88 * YXHeightScale
    }
    
    func lightAButton(blockIdx : Int,ButtonIdx : Int) -> Void {
        var idx = 0
        for item in self.subviews {
            if item is TitleBlockView {
                if blockIdx == idx {
                    let it = item as! TitleBlockView
                    it.lightAButton(idx: ButtonIdx)
                    return
                }
                idx = idx + 1
            }
        }
    }
    
    func addBlock(idx : Int,title : String,titleArr : Array<String>,multiChoose : Bool,key : String) -> Void {
        let title = TitleBlockView.init(idx: idx, title: title, titleArr: titleArr, multiChoose: multiChoose, key: key)
        title.frame = CGRect.init(origin: CGPoint.init(x: title.frame.origin.x, y: hei), size: title.frame.size)
        if delegated == true {
            title.delegate = self
        }
        hei = hei + title.height()
        self.addSubview(title)
    }
    
    func addBlock(title : String,titleArr : Array<String>,key : String,multiChoose : Bool,extra : Bool = false) -> Void {
        let title = TitleBlockView.init(height: hei, title: title, titleArr: titleArr,key: key,multiChoose: multiChoose,extra: extra)//有高度
        if delegated == true {
            title.delegate = self
        }
        hei = hei + title.height()
        self.addSubview(title)
    }
    
    func configFrame(origin : CGPoint,width : CGFloat) -> Void {
        self.frame = CGRect.init(origin: origin, size: CGSize.init(width: width, height: hei))
    }
    
    func provideAllParam() -> Array<Dictionary<String,Any>> {
        var array : Array<Dictionary<String,Any>> = []
        for item in self.subviews {
            if item is TitleBlockView {
                let itm = item as! TitleBlockView
                array.append(itm.param!)
            }
        }
        return array
    }
    
    private func configHead(title : String)->Void{
        let titleView = UIView.init()
        titleView.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: frame.size.width, height: 88 * YXHeightScale))
        titleView.backgroundColor = UIColor.colorWithHexString(hex: "f8f9fb")//需要修改颜色
        self.addSubview(titleView)
        let lbl = UILabel.init()
        lbl.text = title
        lbl.textColor = UIColor.colorWithHexString(hex: "323232")
        lbl.font = UIFont.boldSystemFont(ofSize: 28 * YXWidthScale)
        let size = lbl.text!.StringSize(font: lbl.font)
        lbl.frame = CGRect.init(origin: CGPoint.init(x: 40 * YXWidthScale, y: 30 * YXHeightScale), size: size)
        self.addSubview(lbl)
    }
    
    //titleView 的代理回调
    func handleClick(clicked : Int) {
        var clickCnt = 0
        for item in self.subviews {
            if let itm  = item as? TitleBlockView {
                if itm.clicked == true {
                    clickCnt = clickCnt + 1
                }
            }
        }
        if clickCnt == numberSeted {
            completionBlock()
        }
    }

}
